﻿using MebApp.Launcher.Views;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MebApp.Launcher
{
    public partial class FormLogin : BaseForm
    {
        public LoginViewModel Model { get; set; }
        public FormLogin()
        {
            InitializeComponent();
            Model = ReadModel<LoginViewModel>() ?? new LoginViewModel();
            this.AcceptButton = this.button1;
            this.Load += FormLogin_Load;
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {
            if (Model.LoginOK)
            {
                this.textBox1.Text = Model.Name;
                this.textBox2.Text = Model.Password;
                this.checkBox1.Checked = Model.Remember;
                if (Model.AutoLogin)
                {
                    this.checkBox2.Checked = Model.AutoLogin;
                    button1_Click(null, null);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                textBox1.Property(x => x.Enabled, false);
                textBox2.Property(x => x.Enabled, false);
                checkBox1.Property(x => x.Enabled, false);
                checkBox2.Property(x => x.Enabled, false);
                button1.Property(x => x.Text, "正在登录...");
                button1.Property(x => x.Enabled, false);
                button2.Property(x => x.Enabled, false);
                var u = this.textBox1.Text;
                var p = this.textBox2.Text;
                var resp = LauncherApp.AppClient.Login(u, p);
                if (resp != null)
                {
                    if (resp.code == 200)
                    {
                        LauncherApp.AppClient.UserID = resp.data;
                        if (Model.Remember)
                        {
                            Model.LoginOK = true;
                            Model.Name = u;
                            Model.Password = p;
                            WriteModel(Model);
                        }
                        this.DialogResult = DialogResult.OK;
                        return;
                    }
                    else
                        MessageBox.Show(resp.msg);
                }
                button1.Property(x => x.Text, "登录");
                button1.Property(x => x.Enabled, true);
                button2.Property(x => x.Enabled, true);
                textBox1.Property(x => x.Enabled, true);
                textBox2.Property(x => x.Enabled, true);
                checkBox1.Property(x => x.Enabled, true);
                checkBox2.Property(x => x.Enabled, true);
            }).Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (new FormRegister().ShowDialog(this) == DialogResult.OK)
            {
                MessageBox.Show("您的申请已提交成功，我们会尽快处理。申请结果将通过邮件发送，请留意邮件通知!");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.Model.Remember = checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.Model.AutoLogin = checkBox2.Checked;
        }
    }
    public class LoginViewModel
    {
        public string Name { get; set; }
        public string Password { get; set; }
        public bool Remember { get; set; }
        public bool AutoLogin { get; set; }
        public bool LoginOK { get; set; }
    }
}
